﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Traceability.Infrastructure
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class
    {
        MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }

        public MyDbContext Context { get { return db; } }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> Add(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }

        public async Task<int> AddRange(IEnumerable<T> list)
        {
            await db.Set<T>().AddRangeAsync(list);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns>返回集合</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> Update(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync();
        }

        public async Task<int> UpdateRange(IEnumerable<T> list)
        {
            db.Set<T>().UpdateRange(list);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 物理删除单个实体
        /// </summary>
        /// <param name="t">要删除的实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Delete(T t)
        {
            db.Set<T>().Remove(t);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 根据ID物理删除单个实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteById(object id)
        {
            var entity = await db.Set<T>().FindAsync(id);
            if (entity != null)
            {
                db.Set<T>().Remove(entity);
                return await db.SaveChangesAsync();
            }
            return 0;
        }

        /// <summary>
        /// 批量物理删除
        /// </summary>
        /// <param name="list">要删除的实体集合</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteRange(IEnumerable<T> list)
        {
            db.Set<T>().RemoveRange(list);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 根据条件物理删除
        /// </summary>
        /// <param name="predicate">删除条件</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteWhere(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            var entities = db.Set<T>().Where(predicate);
            db.Set<T>().RemoveRange(entities);
            return await db.SaveChangesAsync();
        }
    }
}
